//
//  TypeCollectionViewCell.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/14.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "TypeCollectionViewCell.h"
#import "TypeSubCollectionViewCell.h"
#import "UIColor+HexM.h"
#import "UIApplication+SkipTool.h"
#import "MyViewController.h"
#import "TypeDetailViewController.h"
#import "PaintingDetailViewController.h"
#import "WSLWaterFlowLayout.h"
#import "PinterestMainViewController.h"

@implementation TypeCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionV];
}

- (UICollectionView *)collectionV {
    if (!_collectionV) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, (Screen_Width - 30) / 2.5 / 2 * 1 + 20) collectionViewLayout:flow];
        //        _collectionV.backgroundColor =  [UIColor colorWithHexString:@"#ededed"];
        //        _collectionV.backgroundColor = [UIColor blackColor];
        _collectionV.backgroundColor = [[UIColor colorWithHexString:@"#C2DFEA"] colorWithAlphaComponent:0.2];
        _collectionV.scrollEnabled = YES;
    }
    return _collectionV;
}

- (void)initCollectionView{
    self.collectionV.dataSource = self;
    self.collectionV.delegate = self;
    [self.collectionV registerNib:[UINib nibWithNibName:@"TypeSubCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"TypeSubCollectionViewCell"];
}

- (void)setDataSourceArray:(NSMutableArray *)dataSourceArray {
    _dataSourceArray = dataSourceArray;
    if (_dataSourceArray.count > 0) {
        [self initCollectionView];
        [self.collectionV reloadData];
    }
}

#pragma mark --CollectionDelegate--
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        if (self.dataSourceArray.count > 0) {
            return self.dataSourceArray.count + 3;
        }else {
            return 0;
        }
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TypeSubCollectionViewCell *cell = [self.collectionV dequeueReusableCellWithReuseIdentifier:@"TypeSubCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = (TypeSubCollectionViewCell *)[[NSBundle mainBundle] loadNibNamed:@"TypeSubCollectionViewCell" owner:nil options:nil];
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((Screen_Width - 30) / 2.5 , (Screen_Width - 30) / 2.5 / 2 * 1);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
//        UINavigationController *nav = [[UIApplication sharedApplication] visibleNavigationController];
//        TypeDetailViewController *type = [TypeDetailViewController initTypeDetailViewController:@"分类啊"];
//        [nav pushViewController:type animated:YES];
        
        UINavigationController *nav = [[UIApplication sharedApplication] visibleNavigationController];
        PinterestMainViewController *flowLayoutPinterest = [NSClassFromString(@"PinterestMainViewController") new];
        flowLayoutPinterest.flowLayoutStyle = WSLWaterFlowVerticalEqualWidth;
        flowLayoutPinterest.hidesBottomBarWhenPushed = YES;
        [nav pushViewController:flowLayoutPinterest animated:YES];
    }
}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if (kind == UICollectionElementKindSectionHeader) {
//        if (indexPath.section == 0 && bannerArray.count > 0) {
//            BannerReusableView *bannerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BannerReusableView" forIndexPath:indexPath];
//            if (!bannerView) {
//                bannerView = [[BannerReusableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width / 728 * 426)];
//            }
//            bannerView.banerArray = bannerArray;
//            return bannerView;
//        }else {
//            return nil;
//        }
//    }
//    return nil;
//}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        if (bannerArray.count > 0)  {
//            return CGSizeMake(Screen_Width, Screen_Width / 728 * 426);
//        }
//    }
//    return CGSizeZero;
//}

#pragma mark --CollectionLayout--
//定义整个CollectionViewCell与整个View的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);//（上、左、下、右）
}


////定义每个UICollectionView的横向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 20;
//}

////定义每个UICollectionView的纵向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
//{
//    return 5;
//}



@end
